<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	<style type="text/css">
	 .errorText
	 {
	     color:red;
	 }
	</style>
</head>
<body>
	<form id="form1" method="post">
        <div style="padding-left:50px;padding-bottom:15px;">
            <table style="table-layout:fixed;" cellspacing="0" cellpadding="5">
            	<tr>
            		<td style="width:70px;">产品类型：</td>
                    <td style="width:180px;">    
                        <input name="medicineType" class="mini-combobox" required="true" valueField="keyName" style="width: 300px" textField="keyValue" url="dictionary_wm_key_val_view.do?typeName=MEDICINE_TYPE" />
                    </td>
            	</tr>
            	<tr>
            		<td style="width:70px;">编号：</td>
                    <td style="width:180px;">    
                        <input name="serialNum" onblur="judgeSerialNum();" errorMode="none" onvalidation="onserialNumValidation"  requiredErrorText="编号不能为空"  class="mini-textbox" required="true" vtype="maxLength:50" style="width: 300px" emptyText="请确保编号的唯一性" />
                    </td>
                 	<td id="serialNum_error" class="errorText" ></td>
            	</tr>
            	<tr>
            		<td style="width:70px;">名称：</td>
                    <td style="width:180px;">    
                        <input id="medicalName" name="name" errorMode="none" onvalidation="checkValidation" requiredErrorText="名称不能为空"  class="mini-textbox" required="true" vtype="maxLength:255" style="width: 300px" emptyText="请输入药品名称并注明其规格"/>
                    </td>
                    <td id="name_error" class="errorText" ></td>
                </tr>
                <tr>
                    <td style="width:70px;">别名：</td>
                    <td style="width:320px;">    
                        <input name="aliases" errorMode="none" onvalidation="checkValidation" requiredErrorText="别名不能为空"  class="mini-textbox" required="true" vtype="maxLength:100" style="width: 300px"/>
                    </td>
                    <td id="aliases_error" class="errorText" ></td>
                </tr>
                <tr>
                    <td style="width:70px;">价格：</td>
                    <td>    
                        <input name="price" class="mini-textbox"  minValue="0" maxValue="99999" format="n2" required="true" style="width: 280px"/>（元）
                    </td>
                    <td id="price_error" class="errorText" ></td>
                </tr>
                <tr>
               		<td style="width:80px;">生产厂家：</td>
                    <td style="width:180px;">
                   		<input name="manufacturer" errorMode="none" onvalidation="checkValidation" requiredErrorText="生产厂家不能为空"  class="mini-textbox" required="true" vtype="maxLength:255" style="width: 300px"/>
                	</td>
                	<td id="manufacturer_error" class="errorText" ></td>
                </tr>
                <tr>
                	<td>有效期：</td>
                	<td style="width:180px;">
                		<input name="valid" class="mini-spinner"  minValue="1" maxValue="120" required="true" style="width: 280px"/>（月）
                	</td>
                	<td id="valid_error" class="errorText" ></td>
                </tr>
                <tr>
                	<td>类别：</td>
                	<td style="width:180px;">    
                    	<input id="btnEdit1" name="medicineSortId" errorMode="none" onvalidation="checkValidation" requiredErrorText="请选择类别" class="mini-buttonedit" required="true" 
                    	 onbuttonclick="onButtonEdit" style="width: 300px" emptyText="请选择药品类别" allowInput="false"/>
                	</td>
                	<td id="medicineSortId_error" class="errorText" ></td>
                </tr>
                <tr>
                	<td style="width:80px;">单位：</td>
                    <td>
                    	<input id="medicine_unit" emptyText="请选择..." name="unit" errorMode="none" onvalidation="checkValidation" requiredErrorText="请选择单位" class="mini-combobox" required="true" valueField="keyName" style="width: 300px" textField="keyValue" url="" />
                    </td>
                    <td id="unit_error" class="errorText" ></td>
                </tr>
                <tr>
                    <td >适应症：</td>
                    <td colspan="3">    
                        <input name="indications" class="mini-textarea" vtype="maxLength:255" style="width:300px;" />
                    </td>
                </tr>
                <tr>
                    <td >用法用量：</td>
                    <td colspan="3">    
                        <input name="dosage" class="mini-textarea" vtype="maxLength:255" style="width:300px;" />
                    </td>
                </tr>
                <tr>
                    <td >主要成分：</td>
                    <td colspan="3">    
                        <input name="mainIngredient" class="mini-textarea" vtype="maxLength:255" style="width:300px;" />
                    </td>
                </tr>
                <tr>
                    <td >禁忌：</td>
                    <td colspan="3">    
                        <input name="avoid" class="mini-textarea" vtype="maxLength:500" style="width:300px;" />
                    </td>
                </tr>
            </table>
        </div>
        <div style="text-align:center;padding:10px;">               
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:50px;">确定</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>       
        </div>        
    </form>

<script type="text/javascript">
	mini.parse();
	
	//加载单位字典表数据
	var type = mini.get(medicine_unit);
	type.load('dictionary_wm_key_val_view.do?typeName=MEDICINE_UNIT_TYPE');
	
	//弹出选择药品类别树
	function onButtonEdit(e) {
	    var btnEdit = this;
	    mini.open({
	        url: "medicine_select_sort.do",
	        showMaxButton: false,
	        title: "选择药品类别",
	        width: 350,
	        height: 350,
	        ondestroy: function (action) {                    
	            if (action == "ok") {
	                var iframe = this.getIFrameEl();
	                var data = iframe.contentWindow.GetData();
	                data = mini.clone(data);
	                if (data) {
	                    btnEdit.setValue(data.id);
	                    btnEdit.setText(data.name);
	                }
	            }
	        }
	    });            
	}
	
	var form = new mini.Form("form1");
	
	//保存数据
	function SaveData() {
	    var o = form.getData(true); 
	    form.validate();
	    if (form.isValid() == false || judgeSerialNum() == false) return;
	  //验证输入的名称是否已经存在
	  var medicalName = mini.get("medicalName").getFormValue();
	  $.post("medicine_name_repeat.do",{name:medicalName},function(data){
		  if(data.result){
			  mini.confirm("已找到重复的药品名，确定继续添加？", "确定？",function (action) {
      			if (action == "ok") {
      				$.ajax({
    			        url: "medicine_save.do",
    					type: 'post',
    					dataType: 'json',
    			        data: o,
    			        cache: false,
    			        success: function (text) {
    			            if(text.success){
    			            	window.CloseOwnerWindow("succ");
    			            }else{
    			            	mini.alert("数据错误！");
    			            }
    			        }
    			    });
      			}
			  });
		  }else{
			  $.ajax({
			        url: "medicine_save.do",
					type: 'post',
					dataType: 'json',
			        data: o,
			        cache: false,
			        success: function (text) {
			            if(text.success){
			            	window.CloseOwnerWindow("succ");
			            }else{
			            	mini.alert("数据错误！");
			            }
			        }
			    });
		  }
	  },"json");
	}
	
	//判断编号是否重复
    function judgeSerialNum(){
    	var serialNum = mini.getbyName('serialNum').getValue();
    	var flag = true;
    	if(serialNum.length > 0){
	  	    $.ajax({
	  	    	url: "medicine_ver_serialnum.do?serialNum=" + serialNum,
	  	    	dataType: 'json',
	  	        cache: false,
		    	async: false,
	 	        success: function (text) {
	 	            if(text.result){
	 	            	flag = true;
	 	            }else{
	 	            	mini.alert("编码重复请修改");
	 	            	flag = false;
	 	            }
	 	        }
	  	    });
    	}
  	    return flag;
    }

    function GetData() {
        var o = form.getData();
        return o;
    }
    
    function CloseWindow(action) {            
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();            
    }
    
    function onOk(e) {
        SaveData();
    }
    
    function onCancel(e) {
        CloseWindow("cancel");
    }
    
    function onserialNumValidation(e) {
		var id =e.sender.name + "_error";
		var el = document.getElementById(id);
		checkValidation(e);
        if (e.isValid) {
            if (isEnglishAndNumber(e.value) == false) {
                e.errorText = "必须输入英文+数字";
                el.innerHTML =e.errorText;
                e.isValid = false;
            }
        }
    }
    
    function isEnglishAndNumber(v) {
        var re = new RegExp("^[0-9a-zA-Z\_]+$");
        if (re.test(v)) return true;
        return false;
    }
    
    function updateError(e) {
        var id =e.sender.name + "_error";
        var el = document.getElementById(id);
        if (el) {
            el.innerHTML =e.errorText;
        }
    }
    
    function checkValidation(e) {                  
        updateError(e);
    }
</script>
</body>
</html>